package com.cib.process.chat.core;

import com.cib.process.bean.IvrRequestServer;
import com.cib.process.bean.QueryRequestParam;
import com.cib.process.chat.core.policy.AbstractActionErrorPolicy;
import com.cib.process.chat.core.policy.ActionErrorPolicyFactory;
import com.cib.process.util.LoggerHelper;
import org.apache.commons.lang.StringUtils;

import java.util.Map;

public abstract class AbstractActionSeletor {
    public abstract void paramSelector(IvrRequestServer ivrRequestServer, QueryRequestParam queryRequestParam);
    public abstract void setCollectConversation(QueryRequestParam queryRequestParam);
    protected abstract String setQueryText(Map<String, String> lastResult);

    public void handleLastResult(IvrRequestServer ivrRequestServer,QueryRequestParam queryRequestParam){
        Map<String,String> lastResult=ivrRequestServer.getLastResult();
        String resultCode = lastResult.get("resultCode");   //ivr返回结果标志
        String resultmessage = lastResult.get("resultmessage");//返回信息描述

        //继承的秒用
        setCollectConversation(queryRequestParam); //设置会话标注(使ngd内敏感信息不可见)

        String queryText=setQueryText(lastResult);
        if (StringUtils.isBlank(resultCode)||"0".equals(resultCode)){
            LoggerHelper.info(this.getClass(),"[sessionId:{}] [userId:{}] [ivr {} handler input:{}]",ivrRequestServer.getSessionId(),ivrRequestServer.getUserId(),ivrRequestServer.getLastAction(),queryText);
            queryRequestParam.setQueryText(StringUtils.isNotBlank(lastResult.get("callaction"))?"":queryText);
        }else{
            //ivr处理失败
            LoggerHelper.err(this.getClass(),"[sessionId:{}] [userId:{}] [ivr {} handler fail resultcode:{},resultmessage{}",ivrRequestServer.getSessionId(),ivrRequestServer.getUserId(),ivrRequestServer.getLastAction(),resultCode,resultmessage);

            //通过相关指令指定策略
            queryRequestParam.setQueryText(queryText);
            AbstractActionErrorPolicy policy= ActionErrorPolicyFactory.getPolicy(ivrRequestServer.getLastAction());
            policy.queryTextHandle(ivrRequestServer,queryRequestParam);
        }
    }


}
